package com.aquila.bible.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aquila.bible.MyBaseActivity;
import com.aquila.bible.R;
import com.aquila.bible.ReadHistoryObject;
import com.aquila.bible.SearchResultObject;
import com.aquila.bible.system.SystemConfig;

public class ChapterActivity extends MyBaseActivity implements OnClickListener {
	
	private TextView titleTextView ;
	private GridView gridView ;
//	private Button prevButton, nextButton ;
	
	
	private int totalChapter ;
//	private int bookIndex ;
	private ChapterAdapter adapter ;
	private String title ;
//	private boolean isChoose = false ; //判断是否是从正文中选择的跳转
//	private boolean isNewTestament = false ;
	private SearchResultObject readHistoryObject ;
	private boolean isSelecte = false ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_chanpter_list_layout);
		titleTextView = (TextView) findViewById(R.id.tv_chapter_title) ;
		gridView = (GridView) findViewById(R.id.gv_chapter) ;
		
		isSelecte = getIntent().getBooleanExtra("isSelecte", false) ;
		Bundle bundle = getIntent().getExtras() ;
		if (bundle != null) {
			readHistoryObject = (SearchResultObject) bundle.getSerializable("object") ;
			title = readHistoryObject.getBookString() ;
			totalChapter = readHistoryObject.getChapterCount();
		}
		
		titleTextView.setText(title) ;
		adapter = new ChapterAdapter() ;
		gridView.setAdapter(adapter) ;
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
//				showToast("点击了" + (position+1), false) ;
				gotoReadText(position) ;
			}
		}) ;
		
	}
	
	
	@Override
	public void onClick(View v) {
	}
	
	
	/**
	 * 
	 * @param position
	 *2012-7-26 下午3:33:13
	 */
	private void gotoReadText(int position){
		Intent intent = new Intent() ;
		intent.setClass(this, TextByListActivity.class) ;
		intent.putExtra("chapterIndex", position + 1) ;
		readHistoryObject.setChapterIndex(position + 1) ;
		if (isSelecte) {
			setResult(123, intent) ;
		}
		else {
			Bundle bundle = new Bundle() ;
			bundle.putSerializable("object", readHistoryObject) ;
			intent.putExtras(bundle) ;
			startActivity(intent) ;
		}
		onBackPressed() ;
	}
	
	
	

	
	class ChapterAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return totalChapter;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View arg1, ViewGroup arg2) {
			LinearLayout layout = (LinearLayout) layoutInflater.inflate(R.layout.chapter_item, null) ; 
			TextView chapterTextView = (TextView) layout.findViewById(R.id.tv_chapter_index) ;
			chapterTextView.setText("第" + (position + 1) + "章") ;
			return layout;
		}
		
	}
	
	
}
